import {getUserCache} from "@/views/order/modules/user";
import {deepCopy} from "@/utils";

export default {
  data(){
    return {
      userAccountList : [],
      userAccountGroup1: [], // 多店铺的用户
      userAccountGroup2: [], // 单店铺的用户
    };
  },
  props:{
    filterStop: {
      type: Boolean,
      default: false,
    }
  },
  mounted() {
    this.getUser()
  },
  methods:{
    getUser(){
      getUserCache().then(res=>{
        this.userAccountList = [];

        const userIds = [];
        res.forEach(item => {
          if(this.filterStop && item.status !== 1){
            return;
          }
          const obj = deepCopy(item)
          obj.check = true;
          userIds.push(obj.id)
          this.userAccountList.push(obj)
        })

        this.userAccountGroup1 = [];
        this.userAccountGroup2 = [];
        this.userAccountList.forEach(item=>{
          if(item.accountList.length > 1){
            const obj = {
              id: item.id,
              account: item.account,
              indeterminate: false,
              check: true,
              accountList: [],
            }
            item.accountList.forEach(account => {
              const obj2 = deepCopy(account)
              obj2.check = true;
              obj.accountList.push(obj2)
            })
            this.userAccountGroup1.push(obj)
          }else{
            const obj = {
              id: item.id,
              account: item.account,
              check: true,
              nickname: item.accountList[0] ? item.accountList[0].nickname : '',
            }
            this.userAccountGroup2.push(obj)
          }
        })
        // 发送一个事件给父组件 可以去请求数据了
        this.$emit('input',userIds)
        this.$emit('apiUserFinish',true);
      })
    },
  }
}
